﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.Model;
using ZF.BLL;
using GCELibrary.Current;
using ZF.BLL.Linq;
using ZF.DAL;
using System.Web.UI.HtmlControls;
namespace ZF.WS.shop
{
    public partial class list : BLL.Page.PageBase
    {
        [PageArgument(Key = "ptid", DefaultValue = 0)]
        public int Ptid { get; set; }
        [PageArgument(Key = "ctid", DefaultValue = 0)]
        public int Ctid { get; set; }

        [PageArgument(Key = "tn", DefaultValue = null)]
        public string tn { get; set; }


        [PageArgument(Key = "pi", DefaultValue = 0)]
        public int Pi { get; set; }


        [PageArgument(Key = "pmin", DefaultValue = 0)]
        public int pmin { get; set; }
        [PageArgument(Key = "pmax", DefaultValue = 0)]
        public int pmax { get; set; }
        [PageArgument(Key = "key", DefaultValue = null)]
        public string key { get; set; }


        protected string _param;
        protected void Page_Load(object sender, EventArgs e)
        {


            var typeGroup = DictionaryConfig.MTypeJsonList.GroupBy(p => p.MType.ParentId);
            //this.lvPTitle.DataSource = DictionaryConfig.MTypeJsonList.Where(p=>p.MType.ParentId==0).Select(p => new
            //{
            //    Key = p.MType.MerchandiseTypeId,
            //    Text = p.MType.Name,
            //    ParentId = p.MType.ParentId,
            //});
            //this.lvPTitle.DataBind();
            //if (Ptid != 0)
            //{
               
            //    var tlist = typeGroup.FirstOrDefault(p => p.Key == Ptid);
            //    if (tlist != null)
            //    {
            //        var ctlist = tlist.Select(p => new
            //        {
            //            Key = p.MType.MerchandiseTypeId,
            //            Text = p.MType.Name,
            //            ParentId=p.MType.ParentId,
            //        }).ToList();
            //        this.lvCTitle.DataSource = ctlist;
            //        this.lvCTitle.DataBind();
            //    }
            //}

            using (var dc = SiteInfo.CreateDBDataContext())
            {
                string param = "?";
                foreach (var name in Request.QueryString.AllKeys)
                {
                    if (name != null && !name.EqIgnoreCase("pi"))
                    {
                        param += "&" + name + "=" + HttpUtility.UrlEncode(Request.QueryString[name]);
                    }
                }

                _param = "/shop/list.aspx" + param;

                var pagesize = 10;
                var sel=dc.SP_Merchandise.Where(p => p.IsSell&&!p.IsDel);
                if (this.Ptid != 0)
                {
                    var ct = typeGroup.FirstOrDefault(p => p.Key == this.Ptid);
                    if (ct != null)
                    {
                        var cids = ct.Select(p => p.MType.MerchandiseTypeId).ToArray();
                        sel = sel.Where(p => (cids.Contains(p.MerchandiseTypeId) || p.MerchandiseTypeId == this.Ptid) && !p.IsDel);
                    }
                    else
                    {
                        sel = sel.Where(p =>  p.MerchandiseTypeId == this.Ptid );
                    }
                    
                }
                if (this.Ctid != 0)
                {
                    sel = sel.Where(p =>p.MerchandiseTypeId==this.Ctid);
                }

                if (tn != null)
                {
                    var tt=DictionaryConfig.MTypeJsonList.FirstOrDefault(p => p.MType.Name == tn);
                    if (tt != null)
                    {
                        sel = sel.Where(p => p.MerchandiseTypeId == tt.MType.MerchandiseTypeId);
                        if (tt.MType.ParentId == 0)

                            this.Ptid = tt.MType.MerchandiseTypeId;
                        else
                            this.Ctid = tt.MType.MerchandiseTypeId;
                    }
                }

 
                if (pmin > 0)
                    sel = sel.Where(p => p.CPrice >= pmin);
                if (pmax > 0)
                    sel = sel.Where(p => p.CPrice <= pmax);
                if (!string.IsNullOrEmpty(key))
                    sel = sel.Where(p => p.Name.Contains(key));


                var rowcount = sel.OrderByDescending(p => p.MerchandiseId).Count();
                if (rowcount > 0)
                {
                    var pagecount = rowcount / pagesize;
                    if (rowcount % pagesize != 0)
                        pagecount++;
                    if (Pi >= pagecount)
                        Pi = pagecount - 1;
                    var list = sel.OrderByDescending(p => p.MerchandiseId)
                            .OrderByDescending(p => p.OrderID)
                            .OrderByDescending(p => p.IsHot)
                            .OrderByDescending(p => p.IsRecommend)
                            .Skip(Pi * pagesize)
                            .Take(pagesize).Select(p => new
                            {
                                Merc = p,
                            }).ToList();
                    this.lvList.DataSource = list;
                    this.lvList.DataBind();
                }
            }
          
        }
    }
}
